﻿#--------------------------------------------------------------------------------------------------------------------------------------------------------
#
# Mi-DevEnv			:	http://midevenv.codeplex.com 
#					:	(c) IT Bridge 2012
#					:	A concept originated by Chris Nurse
#
# Licence			:	GNU General Public License version 2 (GPLv2)
#					:	http://midevenv.codeplex.com/license
#
# Summary           :   Build a server and join it to the domain
#
#--------------------------------------------------------------------------------------------------------------------------------------------------------

Set-StrictMode -Version Latest

if ((Get-Module MDE-Common) -eq $null) {
    Import-Module MDE-Common
    }

[string]$executionPath = Split-Path $script:MyInvocation.MyCommand.Path -Parent

Function InstallProduct() {
    SetIpConfiguration
    Install-ServerFeatures "$executionPath\ServerFeatures.csv"    
    JoinDomain
    VerifyTasks
    }

#region Framework

Function GetInstallName() {
    return "Mi-DevEnv Server"
    }

Function GetInstalled() {
    $dc = Get-DomainControllerName

    if (! ([string]::IsNullOrEmpty($dc)) ) {
        return $true
        }
    else {
        return $false
        }
    }

Function GetPackageName() {
    return "WS"
    }

Function GetPackageDescription() {
    return "Windows Server"
    }

Function GetServerBuild() {
    return $true
    }
    
Function GetInstallPriority() {
    return 2000
    }

Function GetRequireRestart() {
    return $true
    }

#endregion

#region Tasks

Function SetIpConfiguration() {

    $ipConfig = Get-WmiObject -class Win32_NetworkAdapterConfiguration -Filter IPEnabled=TRUE

    if ($ipConfig.SetDNSServerSearchOrder -contains "MDE_DC_STATIC_IP") {
        return 0
        }

    Write-Progress "Verifying IP settings and connectivity"

    if (!(Test-Connection MDE_STATIC_GATEWAY -Quiet -Count 1)) {
        Write-Progress "WARNING: Failed to ping the default gateway MDE_STATIC_GATEWAY"
        }

    Write-Progress "Assigning gateway address : MDE_STATIC_GATEWAY"
            
    $ipConfig.SetGateways("MDE_STATIC_GATEWAY", 1) | Out-Null
    $ipConfig.SetDNSServerSearchOrder("MDE_DC_STATIC_IP") | Out-Null

    Write-Progress "DNS address set : MDE_DC_STATIC_IP"
    return 1
    }
    
Function JoinDomain() {
    Write-Progress "Joining Domain : MDE_DOMAIN_NAME"
    $credential = New-Object System.Management.Automation.PsCredential("MDE_DOMAIN_NAME\Administrator",(ConvertTo-SecureString "MDE_DEFAULT_PASSWORD" -AsPlainText -Force))
    Add-Computer -Credential $Credential -DomainName MDE_DOMAIN_NAME 
    }

Function VerifyTasks() {
    $completeJobs = 0
    $totalJobs = 2

    $hasIIS = Get-FeatureInstalled Application-Server
    $ipConfig = Get-WmiObject -class Win32_NetworkAdapterConfiguration -Filter IPEnabled=TRUE

    if ($ipConfig.DefaultIPGateway -eq "MDE_STATIC_GATEWAY") {
        $completeJobs ++
        Write-Progress "VERIFIED : IP Configuration"
        }
    else {
        Write-Progress "FAILED : IP Configuration"
        }

    if ($hasIIS -eq $true) {
        $completeJobs ++
        Write-Progress "VERIFIED : IIS installed"
    }

    if ($completeJobs -ne $totalJobs) {
        Write-Progress "PROCESS FAILED   : Build Web Server"
        }
    else {
        Write-Progress "PROCESS COMPLETE : Build Web Server"
        }
    }

#endregion